package com.hs.core.db;

 
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class AccessdbUtil {
   private static AccessdbUtil dbutil = new AccessdbUtil();
   
   
   private AccessdbUtil(){
	   
   }
   public static AccessdbUtil getInstance(){
	   if(null == dbutil)
		   dbutil = new AccessdbUtil();
	   return dbutil;
   }
   public Connection getConnetion(){
	   Connection con = null; 
		 try {
				Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
				String dburl ="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=温病大成.mdb";//此为NO-DSN方式
				//载入驱动程序类别
				con = DriverManager.getConnection(dburl); //建立数据库链接,jspdata为ODBC数据源名称
		//		建立Statement对象
 
			} catch (Exception e) { 
				e.printStackTrace();
				 
			}  
			return con;
			 
   }
   public Connection getConnetion(String fileName){
	   Connection con = null; 
		 try {
				Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
				String dburl ="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+fileName;//此为NO-DSN方式
				//载入驱动程序类别
				con = DriverManager.getConnection(dburl); //建立数据库链接,jspdata为ODBC数据源名称
		//		建立Statement对象
 
			} catch (Exception e) { 
				e.printStackTrace();
				 
			}  
			return con;
			 
   }
   
   public void dataProcess(){
//	      Connection con = null;
//	      Statement stm = null;
//	      ResultSet rs = null;
//	      List oldData = new ArrayList();
//	    try{
//	         //1. Register jdbc drivers
//	         Class.forName("oracle.jdbc.driver.OracleDriver");
//	         //2. Create database connection through DriverManager
//	         con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:mydb", "BSPM","PASSWORD");
//	         //3. Prepare statement
//	         stm = con.createStatement();
//	         //4. Execute statement
	          
	   			Connection con = getConnetion("D:\\001.mdb");
	         try {
	 			if(null != con){
	 				    Statement st = con.createStatement();
	 					ResultSet rs = st.executeQuery("select * from t_d_projectword");
	 					int j = 0;
	 			        while(rs.next()){
	 			        	byte[] data = new byte[2048];

	 			        	 j++;
	 			        	 System.out.println(rs.getString("xmID")); 
	 			        	
	 			        	InputStream in =rs.getBinaryStream("mbrw");

	 			        	 

                            File file = new File("d:\\t_"+j+".doc");

                            OutputStream out;
							try {
								out = new BufferedOutputStream(

								              new FileOutputStream(file));
							

                            byte[] buff = new byte[1024];

                            for (int i = 0; (i = in.read(buff)) > 0;) {

                                   out.write(buff, 0, i);

                            }

                            out.close();

                            in.close();
							} catch (FileNotFoundException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
 
	 			        }
	 			}
	 	} catch (SQLException e) {
	 		// TODO Auto-generated catch block
	 		e.printStackTrace();
	 	}
//	       }catch(Exception e){ e.printStackTrace();}
//	       finally{//6. release resources
//	          if(rs != null) try{ rs.close(); }catch(Exception e){}
//	          if(stm != null) try{ stm.close(); }catch(Exception e){}
//	          if(con != null) try{ con.close(); }catch(Exception e){}
//	       }
   }
//   public List getDataList(String fileName,String tabName,String yonghu){
//	   Connection con = getConnetion(fileName);
//	   List dataList = new ArrayList();
//	   String sql = "select * from "+ tabName;
//	   System.out.println(sql);
//	   try {
//			if(null != con){
//				    Statement st = con.createStatement();
//					ResultSet rs = st.executeQuery(sql);
//			        while(rs.next()){
//			        	DataObj obj = new DataObj();
//			        	obj.setId(rs.getString("编号"));
//			        	obj.setJibing(rs.getString("疾病"));
//			        	obj.setZhenghou(rs.getString("证候"));
//			        	obj.setBingyinbingji(rs.getString("病因病机"));
//			        	obj.setZhengzhuang(rs.getString("症状"));
//			        	obj.setZhifa(rs.getString("治法"));
//			        	obj.setFangji(rs.getString("方剂"));
//			        	obj.setZhongyao(rs.getString("中药"));
//			        	obj.setYonghu(yonghu);
//			        	obj.setNeirong(rs.getString("内容"));
//			        	dataList.add(obj);
//			        }
//			}
//	} catch (SQLException e) {
//		// TODO Auto-generated catch block
//		e.printStackTrace();
//	}
//	   return dataList;
//   }
   /**
    * 判断错误记录中是否存在该记录
    * @param fileName
    * @param tabName
    * @param id
    * @return
    */
   public boolean isExsits(String id,String yonghu){
	   String fileName = "问题数据.mdb";
	   String tabName = "erroData";
	   boolean flag = false;
	   Connection con = null;
	   try {
		   con = getConnetion(fileName);
		   String sql = "select * from "+tabName+" where ID = '" + id + "' and 用户='" + yonghu + "'";
		   if(null != con){
			   Statement st = con.createStatement();
			   ResultSet rs = st.executeQuery(sql);
			   if(rs.next())
				   flag = true;
		   }
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		if(null != con)
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	   return flag;
   }
//   /**
//    * 将有问题的数据存到数据表中
//    * @param fileName
//    * @param tabName
//    * @param obj
//    */
//   public void insertOne(DataObj obj){
//	   String fileName = "问题数据.mdb";
//	   String tabName = "erroData";
//	   Connection con = null;
//	   try {
//		   con  = getConnetion(fileName);
//		   String sql = "insert into " + tabName + "(用户,ID,疾病,证候,病因病机,症状,治法,方剂,中药,内容) values('"
//		                 +obj.getYonghu()+"','"+obj.getId()+"'," ;
//		                 if(null != obj.getJibing())
//		                 	sql+="'"+obj.getJibing()+"',";
//		                 else
//		                	sql+=null + ",";
//		                 if(null != obj.getZhenghou())
//			                 	sql+="'"+obj.getZhenghou()+"',";
//			                 else
//			                	sql+=null + ",";
//		                 if(null != obj.getBingyinbingji())
//			                 	sql+="'"+obj.getBingyinbingji()+"',";
//			                 else
//			                	sql+=null + ",";
//		                 if(null != obj.getZhengzhuang())
//			                 	sql+="'"+obj.getZhengzhuang()+"',";
//			                 else
//			                	sql+=null + ",";
//		                 if(null != obj.getZhifa())
//			                 	sql+="'"+obj.getZhifa()+"',";
//			                 else
//			                	sql+=null + ",";
//		                 if(null != obj.getFangji())
//			                 	sql+="'"+obj.getFangji()+"',";
//			                 else
//			                	sql+=null + ",";
//		                 if(null != obj.getZhongyao())
//			                 	sql+="'"+obj.getZhongyao()+"',";
//			                 else
//			                	sql+=null + ",";
//		                 if(null != obj.getNeirong())
//			                 	sql+="'"+obj.getNeirong()+"'";
//			                 else
//			                	sql+=null + "";
//		                 
//		                	  
//		                 sql+=")";
//		   System.out.println("问题数据记录：" + sql);
//		   if(null != con){
//			   Statement st = con.createStatement();
//			   st.executeUpdate(sql);
//		   }
//	} catch (SQLException e) {
//		// TODO Auto-generated catch block
//		e.printStackTrace();
//	}finally{
//		if(null != con)
//			try {
//				con.close();
//			} catch (SQLException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//	}
//	  
//   }
   /**
    * 记录正确的数据
    * @param fileName
    * @param tabName
    * @param obj
    */
//   public void insertOkData(DataObj obj){
//	   String fileName = "正确数据.mdb";
//	   String tabName = "impData";
//	   Connection con = null;
//	   try {
//		   con = getConnetion(fileName);
//		   String sql = "insert into " + tabName + "(ID_OLD,疾病,证候,病因病机,症状,治法,方剂,中药)" +
//		   		" values('"
//		                 +obj.getId()+"'," ;
//		                 if(null != obj.getJibing())
//		                 	sql+="'"+obj.getJibing()+"',";
//		                 else
//		                	sql+=null + ",";
//		                 if(null != obj.getZhenghou())
//			                 	sql+="'"+obj.getZhenghou()+"',";
//			                 else
//			                	sql+=null + ",";
//		                 if(null != obj.getBingyinbingji())
//			                 	sql+="'"+obj.getBingyinbingji()+"',";
//			                 else
//			                	sql+=null + ",";
//		                 if(null != obj.getZhengzhuang())
//			                 	sql+="'"+obj.getZhengzhuang()+"',";
//			                 else
//			                	sql+=null + ",";
//		                 if(null != obj.getZhifa())
//			                 	sql+="'"+obj.getZhifa()+"',";
//			                 else
//			                	sql+=null + ",";
//		                 if(null != obj.getFangji())
//			                 	sql+="'"+obj.getFangji()+"',";
//			                 else
//			                	sql+=null + ",";
//		                 if(null != obj.getZhongyao())
//			                 	sql+="'"+obj.getZhongyao()+"'";
//			                 else
//			                	sql+=null;
////		                 if(null != obj.getNeirong())
////			                 	sql+="'"+obj.getNeirong()+"',";
////			                 else
////			                	sql+=null + "";
//		                 
//		                	  
//		                 sql+=")";
//		   System.out.println("正确数据记录：" + sql);
//		   if(null != con){
//			   Statement st = con.createStatement();
//			   st.executeUpdate(sql);
//		   }
//	} catch (SQLException e) {
//		e.printStackTrace();
//	}finally{
//		if(null != con)
//			try {
//				con.close();
//			} catch (SQLException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//	}
//	  
//   }
   public static void main(String[] args){
//	   System.out.println(Tools.countToken("口甜$舌红$舌赤淡","$"));
//	   System.out.println(Tools.countToken("辛通$苦降","$"));
	   AccessdbUtil.getInstance().dataProcess();
	   
   }
}
